package dymnd.io;

import java.util.*;
import java.io.*;
public class ServerConfig {

	public int port = 0;
	public int width = 0;
	public int height = 0;
	public int layerCount = 1;
	public ArrayList<String> blockedIPs = new ArrayList<String>();
	
	public ServerConfig()	{
		
	}
	
	public static ServerConfig readFile(String s)	{
		ServerConfig sc = new ServerConfig();
		
		try	{
			FileReader fr = new FileReader(new File(s));
			BufferedReader in = new BufferedReader(fr);
			
			String line = "";
			final String portKey = "port:"; //port to listen on
			final String widthKey = "width:"; //width of the canvas
			final String heightKey = "height"; //height of the canvas
			final String layerCountKey = "layerCount"; //number of layers to initialize with
			final String blockedKey = "block:"; //blocked IPs
			
			while((line = in.readLine()) != null)	{
				if(line.toLowerCase().startsWith(portKey))	{
					String[] buffer = line.split(":");
					sc.port = Integer.parseInt(buffer[1]);
				}
				else if(line.toLowerCase().startsWith(widthKey))	{
					String[] buffer = line.split(":");
					sc.width = Integer.parseInt(buffer[1]);
				}
				else if(line.toLowerCase().startsWith(heightKey))	{
					String[] buffer = line.split(":");
					sc.height = Integer.parseInt(buffer[1]);
				}
				else if(line.toLowerCase().startsWith(layerCountKey))	{
					String[] buffer = line.split(":");
					sc.layerCount = Integer.parseInt(buffer[1]);
				}
				else if(line.toLowerCase().startsWith(blockedKey))	{
					String[] buffer = line.split(":");
					sc.blockedIPs.add(buffer[1]);
				}
				
			}
			
			
		}	catch(Exception x)	{
			x.printStackTrace();
			System.out.println("Error in config file: " + s);
		}
		
		
		return sc;
	}
	
}
